Bind input select element to enum in blazor


In this video we will discuss how to bind input select element to enum type in Blazor.

Gender Enum

We want to bind the Gender enum to Input Select element.

bind input select element to enum in blazor

  • Enum.GetValues() method returns the list of Enum memebers (i.e Male, Female, and Other).
  • Foreach loop creates a select element option for each enum member.
  • The built-in InputSelect component supports binding to an enum out of the box.
<InputSelect @bind-Value="Employee.Gender">
    @foreach (var gender in Enum.GetValues(typeof(Gender)))
    {
        <option value="@gender">@gender</option>
    }
</InputSelect>




© 2020 Pragimtech. All Rights Reserved.